<?php

namespace app\admin\model\order;

use app\admin\model\biz\BizInfo;
use app\admin\model\site\PlayProject;
use app\admin\model\user\Player;
use app\helper\Helper;
use app\service\PayService;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\Model;

/**
 * @property $status 0 用户取消 1 已下单 2 已支付 3拒绝 4进行中 5 已完成
 */
class PlayOrder extends Model
{

    // 表名
    protected $table = 'play_order';

    protected $autoWriteTimestamp = 'datetime';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    protected $dateFormat='Y-m-d H:i:s';
    
    public function createOrder($params){
        $order=new self();
        $project=PlayProject::get($params['project_id']);
        $biz=BizInfo::get($params['biz_id']);
        $player=Player::get(['uid'=>$params['player_id'],'status'=>1]);
        if(!$project) throw new Exception('项目不存在');
        if(!$biz) throw new Exception('商户或地点不存在');
        if(!$player) throw new Exception('陪玩师信息错误');
        if($params['uid']==$params['player_id']) throw new Exception('不能预约自己');
        $order->uid=$params['uid'];
        $order->player_id=$params['player_id'];
        $order->appoint_time=$params['appoint_time'];
        $order->project=$project['name'];
        $order->project_logo=$project['logo'];
        $order->duration=$params['duration'];
        $order->price=$project->price;
        $order->pay_price=bcmul($project->price,$params['duration'],2);
        $order->biz_id=$params['biz_id'];
        $order->xingming=$params['xingming'];
        $order->mobile=$params['mobile'];
        $order->address=$params['address'];
        $order->remark=$params['remark'];
        $order->pay_type=$params['pay_type'];
        $order->latitude=$biz->latitude;
        $order->longitude=$biz->longitude;
        $order->save();
        $order->order_no=Helper::orderNo('P',$order->id);
        $order->save();

        return $order;

    }


    public function cancel($id,$uid){
        $order=self::where(['id'=>$id,'uid'=>$uid])->where('status','<',3)->find();
        if(empty($order)) throw new Exception('订单不存在');
        if($order->status==2){
            //todo 退款
        }
        $order->status=0;
        return $order->save();
    }


    /**
     *
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @throws Exception
     */
    public function refuse($id, $uid){
        $order=self::where(['id'=>$id,'player_id'=>$uid])->where('status','=',2)->find();
        if(empty($order)) throw new Exception('订单不存在');
        //todo 退款
        $order->status=3;
        return $order->save();
    }


    public function accept($id, $uid){
        $order=self::where(['id'=>$id,'player_id'=>$uid])->where('status','=',2)->find();
        if(empty($order)) throw new Exception('订单不存在');
        $order->status=4;
        return $order->save();
    }


    public function detail($id,$uid,$from,$lat=0,$lng=0){

        $where=$from==1?['uid'=>$uid]:['player_id'=>$uid];
        $order=$this->alias('o')->field('*')->with(['info'=>function($query){
            $query->withField('id,name,address,latitude,longitude,contacts,mobile');
        }])->where('o.id',$id)->where($where)->find();
        if(empty($order)) throw new Exception('订单不存在');
        $order->distance=Helper::getDistance($lat,$lng,$order->latitude,$order->longitude);
        $order->player_info=(new Player())->getInfo($order->player_id);
        return $order;

    }

    public function pay($params){
        $order=self::where(['id'=>$params['oid'],'uid'=>$params['uid'],'status'=>1])->find();
        if(!$order) throw new Exception('订单信息错误');
        $title='购买'.$order->project.' '.$order->duration.'个小时';
        $result=PayService::pay($params['paytype'],$order->order_no,$title,$order->pay_price,$params['method'],$params['openid'],$params['auth_code']);
        dump($result);
        return 1;
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'uid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function player()
    {
        return $this->belongsTo('app\admin\model\User', 'player_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function info()
    {
        return $this->belongsTo('app\admin\model\biz\Info', 'biz_id', 'id', [], 'LEFT')->setEagerlyType(1);
    }
}
